﻿using System;
using System.IO;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace RegionSplittor.Utils
{
	public class ProjectDocumentOperator
	{
		public static string CopyDocument(string strSrcPath)
		{
			string result = "";
			if (!File.Exists(strSrcPath))
			{
				return result;
			}
			string text = Path.Combine(Path.GetTempPath(), Path.GetFileName(strSrcPath));
			try
			{
				File.Copy(strSrcPath, text, true);
				result = text;
			}
			catch (Exception)
			{
			}
			return result;
		}

		public static Document NewDocument(UIApplication uiApp, string strTempletPath)
		{
			return uiApp.Application.NewProjectDocument(strTempletPath);
		}

		public static Document OpenDocument(string strSrcPath, UIApplication uiApp)
		{
			if (!File.Exists(strSrcPath))
			{
				return null;
			}
			return uiApp.Application.OpenDocumentFile(strSrcPath);
		}

		public static void CloseDocument(Autodesk.Revit.DB.Document doc)
		{
			if (doc != null)
			{
				doc.Close(false);
				doc.Dispose();
			}
		}

		public static void DeleteDocument(string strPath)
		{
			if (File.Exists(strPath))
			{
				File.Delete(strPath);
			}
		}
	}
}
